package cn.yitun.chat.yitunserverimcore.general.main.biz.chat.manager;

import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.core.chat.GroupChatHistory;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Content;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Item;
import cn.yitun.chat.yitunservercommonlib.dataentity.data.message.chat.Section;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupChatContentVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupChatItemVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.system.user.UserVo;
import cn.yitun.chat.yitunserverimbase.dao.group.GroupChatItemDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 
 * Date 2019-03-08 13:45:37<br>
 * Description 
 * @author yitun<br>
 * @since 1.1
 */
@Service
@Slf4j
public class GroupChatHistoryManager {
	
	@Resource
	private GroupChatItemDao groupChatItemDAO;
//	@Resource
//	private GroupDao groupDao;

	public List<GroupChatHistory> getGroupChatHistoryList(List<GroupChatContentVo> chatContentList, boolean inverted) {

		List<GroupChatHistory> contents = new ArrayList<>();

		List<Long> contentIds = chatContentList.stream().map(GroupChatContentVo::getId).collect(Collectors.toList());

		Map<Long, List<GroupChatItemVo>> chatItemMap = new HashMap<>();
		if (YitunCollectionUtil.isNotEmpty(contentIds)) {
			List<GroupChatItemVo> chatItemList = groupChatItemDAO.getGroupChatItemListByContentIds(contentIds);
			for (GroupChatItemVo rci : chatItemList) {
				List<GroupChatItemVo> list = chatItemMap.get(rci.getGroupChatContentId());
				if (null == list) {
					list = new ArrayList<>();
					chatItemMap.put(rci.getGroupChatContentId(), list);
				}
				list.add(rci);
			}
		}

		for (GroupChatContentVo rcc : chatContentList) {

			Long groupId = rcc.getGroupId();
			Long messageId = rcc.getMessageId();
			Long contentId = rcc.getId();
			
			//TODO
			GroupChatHistory map = new GroupChatHistory();
			Content content = new Content();
			UserVo userData = new UserVo();
			GroupVo groupData = new GroupVo();
			// 聊天用户
			userData.setHead(rcc.getUserHead());
			userData.setId(rcc.getUserId());
			userData.setName(rcc.getUserName());
			userData.setNickname(rcc.getUserNickname());
			userData.setAvatar(rcc.getUserAvatar());
			// 群组信息
			groupData.setHead(rcc.getGroupHead());
			groupData.setAvatar(rcc.getGroupAvatar());
			groupData.setId(rcc.getGroupId());
			groupData.setName(rcc.getGroupName());
			//userData.setRole(rcc.getUserRole());
			List<GroupChatItemVo> list = chatItemMap.get(rcc.getId());
			List<Section> sections = new ArrayList<>();
			if (null != list && !list.isEmpty()) {
				int index = -1;
				Section section;
				List<Item> items = null;
				for (GroupChatItemVo rci : list) {
					if (index != rci.getSection()) {
						index = rci.getSection();

						items = new ArrayList<>();
						section = new Section();
						section.setItems(items);
						sections.add(section);
					}

					Item item = new Item();
					item.setType(rci.getType());
					item.setValue(rci.getFilterValue());
					if (null != items) {
						items.add(item);
					}
				}
				// 设置群聊记录
				content.setSections(sections);
				content.setKey(messageId);
				content.setTimestamp(rcc.getCreateTime());
				content.setId(contentId);
				// 设置群聊记录其它信息
				map.setUser(userData);
				map.setGroup(groupData);
				map.setMessageId(messageId);
				map.setContentId(contentId);
				map.setContent(content);
	
				if (inverted) {
					contents.add(0, map);
				} else {
					contents.add(map);
				}
			}
		}
//		log.info("群聊天：{}", contents);
		return contents;
	}
}
